package StudyCode.summary;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 * @author lixiaoyao
 */
public class GradientFill {
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				JFrame frame = new GradientFillFrame();
				frame.setTitle("DrawTest");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

class GradientFillFrame extends JFrame {

	public GradientFillFrame() {
		add(new DrawComponent());
		pack();
	}
}

class DrawComponent extends JComponent {
	private static final int DEFAULT_WIDTH = 400;
	private static final int DEFAULT_HEIGHT = 400;
	private static final int MARGIN = 5;

	private Rectangle rectangle = new Rectangle(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);

	protected void paintComponent(Graphics graphics) {
		Graphics2D g = (Graphics2D) graphics;

		Composite oldComposite2 = g.getComposite();
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
		Shape oudClipShape2 = g.getClip();
		g.setClip(rectangle);
		g.setPaint(Color.RED);

		double maxValue = Double.compare(rectangle.getBounds().getWidth(), rectangle.getBounds().getHeight()) == 1 ?
				rectangle.getBounds().getWidth() : rectangle.getBounds().getHeight();
		int minX = (int) rectangle.getBounds().getMinX();
		int minY = (int) rectangle.getBounds().getMinY();

		for (int i = 0; i < maxValue; i = i + MARGIN) {
			double x1 = i;
			double y1 = 0;
			double x2 = maxValue;
			double y2 = maxValue - i;
			Point2D p = new Point2D.Double(minX + x1, minY + y1);
			Point2D q = new Point2D.Double(minX + x2, minY + y2);
			g.draw(new Line2D.Double(p, q));
		}
		for (int i = MARGIN; i < maxValue; i = i + MARGIN) {
			double x1 = 0;
			double y1 = i;
			double x2 = maxValue - i;
			double y2 = maxValue;
			Point2D p = new Point2D.Double(minX + x1, minY + y1);
			Point2D q = new Point2D.Double(minX + x2, minY + y2);
			g.draw(new Line2D.Double(p, q));
		}
		g.setClip(oudClipShape2);
		g.setComposite(oldComposite2);
	}

	public Dimension getPreferredSize() {
		return new Dimension(450, 450);
	}
}
